package com.databasir.core.meta.provider.jdbc;

import com.databasir.core.meta.data.IndexMeta;
import com.databasir.core.meta.provider.IndexMetaProvider;
import com.databasir.core.meta.provider.condition.TableCondition;
import lombok.extern.slf4j.Slf4j;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

@Slf4j
public class JdbcIndexMetaProvider implements IndexMetaProvider {
     @Override
    public List<IndexMeta> selectIndexes(Connection connection, TableCondition tableCondition) {

    return null;
    }

    private List<IndexMeta> doCreateIndexDocs(Connection connection,TableCondition tableCondition) throws SQLException {
       String databaseName = tableCondition.getDatabaseName();
       String tableName = tableCondition.getTableName();
       List<IndexMeta> indexMetas = new ArrayList<>();
       ResultSet indexResults;

        try {
            indexResults = connection.getMetaData().getIndexInfo(databaseName, tableCondition.getSchemaName(), tableName, false, false);

        } catch (SQLException e) {
           log.warn("warn: ignore"+databaseName+"."+tableName+",error:"+e.getMessage());
           return indexMetas;
        }
        HashMap<String, IndexMeta> metaGroupByName = new HashMap<>();
        try {
            while (indexResults.next()){
               boolean nonUnique = indexResults.getBoolean("NON_UNIQUE");
                String  indexName = indexResults.getString("INDEX_NAME");
               String columnName = indexResults.getString("COLUMN_NAME");
                if (indexName==null){
                    continue;
                }
                if (metaGroupByName.containsKey(indexName)){
                    metaGroupByName.get(indexName).getColumnNames().add(columnName);
                }else {
                    List<String> columns = new ArrayList<>();
                    columns.add(columnName);
                    IndexMeta indexMeta = IndexMeta.builder()
                            .name(indexName)
                            .columnNames(columns)
                            .isUniqueKey(Objects.equals(nonUnique, false))
                            .build();
                   metaGroupByName.put(indexName,indexMeta);
                }

            }
        } finally {
            indexResults.close();
        }
        return new ArrayList<>(metaGroupByName.values());
    }
}
